﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQData;
using System.Data;
using System.Web.Routing;
using VQWEB.common;
using System.Reflection;
namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for IntroHandler
    /// </summary>
    public class core_IntroductionHandler : IHttpHandler, IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
             #region Introduction     
            string add_introduction = context.Request["add_introduction"];
            if (!string.IsNullOrEmpty(add_introduction))
            {
             BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
             var obj = bussiness.InsertOrUpdate<core_introduction>(add_introduction);
             bussiness.InsertAllInOne<core_category_introduction>(obj.category_array_id, "category_id", "introduction_id", obj.introduction_id);
             bussiness.InsertAllInOne<core_tag_introduction>(obj.tag_array_id, "tag_id", "introduction_id", obj.introduction_id);
             context.Response.Write("Thực thi thành công !");    
             
            }
            string view_introduction = context.Request["view_introduction"];
            if (!string.IsNullOrEmpty(view_introduction))
            {
                string Id = context.Request["Id"];
                if (!string.IsNullOrEmpty(Id))
                    RenderSingle(context, Convert.ToInt32(Id));
                else
                    RenderCategory(context);
            }
            string list_introduction = context.Request["list_introduction"];
            if(!string.IsNullOrEmpty(list_introduction)){
                RenderList(context);   
            }
            string delete_introduction = context.Request["delete_introduction"];
            if (!string.IsNullOrEmpty(delete_introduction)) {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.Delete<core_introduction>(delete_introduction);
                RenderList(context);
            }
            string lockunlock_introduction = context.Request["lockunlock_introduction"];
            if (!string.IsNullOrEmpty(lockunlock_introduction)) {
                BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
                bussiness.LockUnLock<core_introduction>(lockunlock_introduction, "introduction_active");
                RenderList(context);
            }
            #endregion
        }

        private void RenderSingle(HttpContext context,int Id)
        {
            BusinessLayer<CoreModelDataContext> bussiness = new BusinessLayer<CoreModelDataContext>();
            object obj = bussiness.Get<core_introduction>(
               c => c.introduction_id.Equals(Id),
               c => new
               {
                   id = c.introduction_id,
                   title = c.introduction_title,
                   content = c.introduction_content,
                   hit=c.introduction_hit,
                   date=c.introduction_date,
                   active = c.introduction_active
               });
            PropertyInfo info = obj.GetType().GetProperty("id");
            var value = info.GetValue(obj, null);

            List<object> array_category_id = bussiness.List<core_category_introduction>(
                null,
                null,
                n => n.introduction_id.Equals(value),
                null,
                n => n.category_id
                ).ToList();
            List<Dictionary<string, object>> listcategory = bussiness.RenderArrayCheckboxJSON(CoreData.ViewCategory("introduction"), array_category_id, "id", "ten", "category_array_id[]");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("introduction", obj);
            dic.Add("category", listcategory);
            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderList(HttpContext context) {

             DataLayer<CoreModelDataContext> layer = new DataLayer<CoreModelDataContext>();
             IList<object> list = layer.List<core_introduction>(
                 null,
                 null,
                 null,
                 null,
                 c => new
                 {
                     id = c.introduction_id,
                     title = c.introduction_title,
                     content = c.introduction_content,
                     hit = c.introduction_hit,
                     date = c.introduction_date,
                     active = c.introduction_active
                 });
             string json = serializer.Serialize(list);
             context.Request.ContentType = "aplication/json";
             context.Response.Write(json);
            
        }
        private void RenderCategory(HttpContext context)
        {
            BusinessLayer<CoreModelDataContext> business = new BusinessLayer<CoreModelDataContext>();
            var category = business.RenderArrayCheckboxJSON(CoreData.ViewCategory("introduction"), null, "id", "ten", "category_array_id[]");

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("category", category);
            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        public IHttpHandler GetHttpHandler(RequestContext RequestContext) {
            return this;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    
}